//
//  OPMainThemeController.m
//  OnePiece
//
//  Created by wsy on 15/10/26.
//  Copyright © 2015年 wsy. All rights reserved.
//

#import "OPThemeController.h"
#import "OPRequesterManager.h"
#import "MainThemeModel.h"
#import "MainThemeCell.h"
#import "ThemeDetailController.h"
@interface OPThemeController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) NSMutableArray *dataSourceArray;
@property (nonatomic, strong) UICollectionView *collectionView;
@end

@implementation OPThemeController

-(NSMutableArray *)dataSourceArray
{
    if (_dataSourceArray == nil) {
        _dataSourceArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataSourceArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = self.naviTitle;
    UICollectionViewFlowLayout *layOut = [[UICollectionViewFlowLayout alloc]init];
    layOut.itemSize = CGSizeMake((SCREEN_WIDTH-60)/3, (SCREEN_WIDTH-60)/3 + 50);
    
    layOut.minimumInteritemSpacing = 20;
    layOut.minimumLineSpacing = 10;
    layOut.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.navigationController.navigationBar.translucent = NO;
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-114) collectionViewLayout:layOut];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.contentInset = UIEdgeInsetsMake(10, 10, 0, 10);
    _collectionView.bounces = NO;
    
    [self.view addSubview:_collectionView];
    
    [_collectionView registerClass:[MainThemeCell class] forCellWithReuseIdentifier:@"collection"];
    
    [self requestData];
    
    
}


- (void)requestData
{
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    
    [OPRequesterManager requestWithType:GET urlString:@"http://mapi.mafengwo.cn/travelguide/discovery/topics?device_type=android&start=0&app_ver=6.1.0&style=squares&open_udid=ac%3Af7%3Af3%3Aa4%3A73%3Acd&oauth_signature=YJQyDZLKpiTMVe4zlQ1aKbMvzI8%3D&screen_height=1280&oauth_signature_method=HMAC-SHA1&oauth_consumer_key=5&oauth_version=1.0&oauth_timestamp=1445667427&oauth_nonce=00fd6341-a348-4c52-9479-062b91e569b4&hardware_model=MI+2S&device_id=ac%3Af7%3Af3%3Aa4%3A73%3Acd&id=1141&sys_ver=5.0.2&o_lng=121.288733&o_lat=31.128081&channel_id=SLL&screen_width=720&oauth_token=0_0969044fd4edf59957f4a39bce9200c6&screen_scale=2.0&mfwsdk_ver=20140507&x_auth_mode=client_auth&app_code=com.mfw.roadbook" parDic:parDic finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        
        NSArray *array = dic[@"data"][@"list"];
        for (NSDictionary *themeDic in array) {
            MainThemeModel *model = [[MainThemeModel alloc]init];
            [model setValuesForKeysWithDictionary:themeDic];
            [self.dataSourceArray addObject:model];
        }
        
        [_collectionView reloadData];
        
    } error:^(NSError *error) {
        NSLog(@"请求数据");
    }];
    
    
    
}


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataSourceArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MainThemeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collection" forIndexPath:indexPath];
    
    [cell setConfigureModel:_dataSourceArray[indexPath.item]];

    return cell;
    
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ThemeDetailController *TDC = [[ThemeDetailController alloc]init];
    TDC.url = [_dataSourceArray[indexPath.item] jump_url];
    TDC.title = [_dataSourceArray[indexPath.item] title];
    [self.navigationController pushViewController:TDC animated:YES];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
